package cn.hsa.xxx.bookData.bo.impl;


import cn.hsa.xxx.bookData.bo.InventoryBatchBO;
import cn.hsa.xxx.bookData.dao.InventoryBatchDAO;
import cn.hsa.xxx.api.dto.InventoryBatchDTO;
import cn.hsa.xxx.bookData.dto.InventoryBatchDTOConverter;
import cn.hsa.xxx.bookData.entity.InventoryBatchDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class InventoryBatchBOImpl implements InventoryBatchBO {

    @Resource
    InventoryBatchDAO inventoryBatchDAO;

    @Autowired
    InventoryBatchDTOConverter inventoryBatchDTOConverter;

    @Override
    public InventoryBatchDTO add(InventoryBatchDTO inventoryBatchDTO) {
        InventoryBatchDO inventoryBatchDO = inventoryBatchDTOConverter.convertToInventoryBatchDO(inventoryBatchDTO);
        inventoryBatchDAO.insert(inventoryBatchDO);
        return inventoryBatchDTOConverter.convertToInventoryBatchDTO(inventoryBatchDO);
    }

    @Override
    public String delete(String id) {
        inventoryBatchDAO.deleteById(id);
        return "删除成功";
    }

    @Override
    public InventoryBatchDTO get(String id) {
        InventoryBatchDO inventoryBatchDO = inventoryBatchDAO.selectById(id);
        InventoryBatchDTO inventoryBatchDTO = inventoryBatchDTOConverter.convertToInventoryBatchDTO(inventoryBatchDO);
        return inventoryBatchDTO;
    }

    @Override
    public String update(InventoryBatchDTO inventoryBatchDTO) {
        InventoryBatchDO inventoryBatchDO = inventoryBatchDTOConverter.convertToInventoryBatchDO(inventoryBatchDTO);
        inventoryBatchDAO.updateById(inventoryBatchDO);
        return "删除成功";
    }

    @Override
    public List<InventoryBatchDTO> getPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<InventoryBatchDO> inventoryBatchDOList = inventoryBatchDAO.selectList(null);

        PageInfo pageInfo = new PageInfo(inventoryBatchDOList);
        long totalRecords = pageInfo.getTotal();
        long totalPages = pageInfo.getPages();

        List<InventoryBatchDTO> inventoryBatchDTOList = new ArrayList<>();
        for (InventoryBatchDO inventoryBatchDO : inventoryBatchDOList) {
            inventoryBatchDTOList.add(inventoryBatchDTOConverter.convertToInventoryBatchDTO(inventoryBatchDO));

        }

        return inventoryBatchDTOList;
    }
}
